import os
import uuid

from django.core.files.storage import FileSystemStorage
from django.shortcuts import render, redirect

from blog.models import User

#个人信息模块、即用户主页模块功能实现

def insertProductData(request):
    image = request.FILES['image']
    id = request.session['id']
    user = User.objects.get(pk=id)
    fs = FileSystemStorage()
    unique_filename = f"{uuid.uuid4().hex}{os.path.splitext(image.name)[1]}"
    filename = fs.save(unique_filename, image)
    uploaded_file_url = fs.url(filename)
    user.user_img = uploaded_file_url
    user.save()
    return redirect('user')


def user_msg(request):
    id = request.session['id']
    user = User.objects.get(pk=id)
    cars = user.car_set.all()
    return render(request,'user.html',locals())


